﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYView;

namespace YArchitech.HVAC
{
	public partial class FrmDifferentialPressure : System.Windows.Forms.Form
	{
		public FrmDifferentialPressure(Autodesk.Revit.DB.Document Doc)
		{
			this.doc = Doc;
			base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.userSetting = new UserSetting();
			this.readDefault();
		}

		private double ConvertToDouble(string text)
		{
			if (!string.IsNullOrEmpty(text))
			{
				return Convert.ToDouble(text);
			}
			return 0.0;
		}

		private void readDefault()
		{
			this.hyTextBox1.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("DifferentialPressure", "High")) ? "2.7" : this.userSetting.GetValue("DifferentialPressure", "High"));
			this.cbLevel.Checked = (!string.IsNullOrEmpty(this.userSetting.GetValue("DifferentialPressure", "Levels")) && Convert.ToBoolean(this.userSetting.GetValue("DifferentialPressure", "Levels")));
			this.cbByWall.Checked = (!string.IsNullOrEmpty(this.userSetting.GetValue("DifferentialPressure", "Wall")) && Convert.ToBoolean(this.userSetting.GetValue("DifferentialPressure", "Wall")));
		}

		private void writeDefault()
		{
			this.userSetting.WriteValue("DifferentialPressure", "High", this.hyTextBox1.Text);
			this.userSetting.WriteValue("DifferentialPressure", "Levels", this.cbLevel.Checked.ToString());
			this.userSetting.WriteValue("DifferentialPressure", "Wall", this.cbByWall.Checked.ToString());
		}

		private void button1_Click(object sender, EventArgs e)
		{
			List<Level> sortLevels = this.GetSortLevels(this.doc, double.MinValue, double.MaxValue);
			List<ViewPlan> list = new List<ViewPlan>();
			List<double> list2 = new List<double>();
			foreach (Element element in new FilteredElementCollector(this.doc).OfClass(typeof(ViewPlan)))
			{
				ViewPlan viewPlan = element as ViewPlan;
				if (viewPlan != null && (int)viewPlan.ViewType == 1 && viewPlan.GenLevel != null)
				{
					list.Add(viewPlan);
				}
			}
			foreach (ViewPlan viewPlan2 in (from l in list.Distinct(new FrmDifferentialPressure.LevelEqualityComparer()).ToList<ViewPlan>()
			where l.GenLevel.Id != this.doc.ActiveView.GenLevel.Id
			select l).ToList<ViewPlan>())
			{
				list2.Add(viewPlan2.GenLevel.Elevation);
			}
			List<Level> list3 = new List<Level>();
			for (int i = 0; i < sortLevels.Count; i++)
			{
				for (int j = 0; j < list2.Count; j++)
				{
					if (this.IsEqual(sortLevels[i].Elevation, list2[j]) && !list3.Contains(sortLevels[i]))
					{
						list3.Add(sortLevels[i]);
					}
				}
			}
			SelectFloorForm selectFloorForm = new SelectFloorForm(list3);
			selectFloorForm.ShowDialog();
			this.Indexs = selectFloorForm.SelectedViewIndexs();
		}

		private bool IsEqual(double val1, double val2)
		{
			return !this.LessThan(val1, val2) && !this.LessThan(val2, val1);
		}

		private bool LessThan(double val1, double val2)
		{
			return val1 - val2 < -1E-09;
		}

		private List<Level> GetSortLevels(Autodesk.Revit.DB.Document doc, double bottom, double top)
		{
			List<Level> list = new List<Level>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(Level)).ToElements())
			{
				Level level = element as Level;
				if (!(doc.ActiveView.GenLevel.Id == level.Id))
				{
					double elevation = level.Elevation;
					if (elevation >= bottom && elevation <= top)
					{
						list.Add(level);
					}
				}
			}
			list.Sort(new FrmDifferentialPressure.LevelComparer());
			return list;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(this.hyTextBox1.Text))
			{
				this.writeDefault();
				base.DialogResult = DialogResult.OK;
				base.Close();
				return;
			}
			YJKMessageBox.Show("安装高度不能为空！");
			this.hyTextBox1.Focus();
		}

		private void button3_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Close();
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			this.button1.Enabled = this.cbLevel.Checked;
		}

		private UserSetting userSetting;

		private Document doc;

		public List<int> Indexs;

		private const double _epsDouble = 1E-09;

		private class LevelComparer : IComparer<Level>
		{
			public int Compare(Level x, Level y)
			{
				return x.Elevation.CompareTo(y.Elevation);
			}
		}

		private class LevelEqualityComparer : IEqualityComparer<ViewPlan>
		{
			public bool Equals(ViewPlan ele1, ViewPlan ele2)
			{
				return ele1.GenLevel.Elevation == ele2.GenLevel.Elevation;
			}

			public int GetHashCode(ViewPlan obj)
			{
				return obj.ToString().GetHashCode();
			}
		}
	}
}
